package com.example.service;

import com.example.bean.User;
import com.example.mapper.UserMapper;
import com.example.util.ActivationContent;
import com.example.util.GymUtil;
import com.example.util.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MailUtil mailUtil;

    @Autowired
    private TemplateEngine templateEngine;

    @Value("${gym.domain}")
    private String domain;

    @Value("${server.servlet.context-path}")
    private String contextPath;

    public User login(User user) {
        User selectByUsername = userMapper.selectByUsername(user.getUsername());
        if (selectByUsername == null) return null;
        user.setPassword(GymUtil.md5(user.getPassword() + selectByUsername.getSalt()));
        return userMapper.selectByUsernameAndPassword(user);
    }

    public User selectByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    public User selectByEmail(String email) {
        return userMapper.selectByEmail(email);
    }


    public boolean register(User user) {
        //注册用户
        user.setSalt(GymUtil.generateUUID().substring(0, 5));
        user.setPassword(GymUtil.md5(user.getPassword() + user.getSalt()));
        user.setRoleId(user.getRoleId());
        user.setStatus(0);
        user.setActivationCode(GymUtil.generateUUID());
        user.setHeaderUrl(String.format("http://images.nowcoder.com/head/%dt.png", new Random().nextInt(1000)));
        user.setCreateTime(new Date());
        if (userMapper.insertUser(user) == 0) return false;

        if (user.getRoleId() == 0) { //添加管理员 无须激活
            //发送激活邮件
            Context context = new Context();
            context.setVariable("username", user.getUsername());
            context.setVariable("url", domain + contextPath + "result.html?/activation/" + user.getId() + "/" + user.getActivationCode());
            String process = templateEngine.process("/mail/activation", context);
            mailUtil.sendMail(user.getEmail(), "[海大体育馆] Email 地址验证", process);
        }
        return true;
    }

    public int activation(int id, String activationCode) {
        User user = userMapper.selectById(id);
        if (user == null) return -1;
        if (!user.getActivationCode().equals(activationCode)) return -1;
        if (user.getStatus() == 1) return 1;
        userMapper.updateStatus(id, 1);
        return 0;
    }

    public boolean passwordCheck(int id, String password) {
        User user = userMapper.selectById(id);
        return GymUtil.md5(password + user.getSalt()).equals(user.getPassword());
    }

    public void updatePassword(int id, String password) {
        User user = userMapper.selectById(id);
        userMapper.updatePassword(id, GymUtil.md5(password + user.getSalt()));
    }

    public List<User> search(String username, String email, int roleId, int status) {
        username = "%" + username + "%";
        return userMapper.selectByConditions(username, email, roleId, status);
    }

    public void updateUserInfo(User user) {
        userMapper.updateStatus(user.getId(), user.getStatus());
        userMapper.updateRoleId(user.getId(), user.getRoleId());
    }

    public boolean delete(int id) {
        if (userMapper.selectById(id) == null) return false;
        userMapper.deleteUser(id);
        return true;
    }
}
